<?php

namespace App\Http\Controllers\Api\V1\Resources;

use App\Models\BuyerNotice;
use App\Models\SeckillProduct;
use Illuminate\Support\Facades\Redis;
use Illuminate\Http\Resources\Json\JsonResource;

class ProductResource extends JsonResource
{
    public function toArray($request)
    {
    	$resource = $this->resource;
    	$xcImgs = [];
    	if($resource->xc_image){
    		foreach($resource->xc_image as $k=>$v){

    			$xcImgs[$k] =  img_path($v);
    		}
    		
    	}
    	$image  = "";
    	$images = [];
    	if($resource->image){
    		
    		if(is_array($resource->image)){

    			//$resource->image = json_decode($resource->image,true);

    			foreach($resource->image as $k=>$v){

	    			$images[$k] =  img_path($v);
	    			$image = $images[0];
	    		}
    		}else{

    			$image = $resource->image;
    		}

    	}

    	
    	
        $data = [
        	
        	'id' => $resource->id,
	        'title' => $resource->title,
	        'brand' =>$resource->brand->name ?? '',
	        'type' => $resource->type->name ?? '',
	        'price'=> $resource->price ?? '',
	        'age'=>$resource->age ?? '',
	        'mileage'=> $resource->mileage ?? '',
	        'times'=> $resource->times ?? '',
	        'displacement' => $resource->displacement ?? '',
	        'images' => $images,
	        'image' => $image,
	        'xc_image' => $xcImgs,
	        /*'zq_image' => img_path($resource->zq_image),
	        'yh_image' => img_path($resource->yh_image),
	        'xj_image' => img_path($resource->xj_image),*/
	        'description'=> $resource->description ?? '' ,
	        'standard' => $resource->standard->name ?? '',
	         'city' => $resource->city ?? '',
	         'province' => $resource->province ?? '',
	         'user_id' =>$resource->user ? $resource->user->id : '',
	         'user_name'=>$resource->user ? $resource->user->name : '测试',
	         'brand_id'=>$resource->brand_id ?? 0,
	         'brand_model_id'=>$resource->brand_model_id ?? 0,
	         'brand_model_name'=>$resource->brandModel->name ?? '',
	         'type_id'=>$resource->type_id ?? 0,
	         'standard_id'=>$resource->standard_id ?? 0,
	         'finial_price'=> $resource->finial_price ?? 0,
	         'trade_price'=> $resource->trade_price ?? 0,
	         'ls_price'=> $resource->ls_price ?? 0,
	         'is_hidden'=>$resource->is_hidden ?? 0,
	         'is_sale'=>$resource->is_sale ?? 0,
	         'is_open'=>$resource->is_open ?? 0,
	         'is_examine'=>$resource->is_examine ?? 0,
	         'register_time' => $resource->register_time ?? '',
	         'province_code' => $resource->province_code ?? '',
	         'city_code' => $resource->city_code ?? '',


        ];
        
        return $data;
    }
}
